$(function () {
    var record_datagrid = $("#record_datagrid")
    var record_dialog = $("#record_dialog")
    var productItem_datagrid = $("#productItem_datagrid")
    productItem_datagrid.datagrid({
        //url:'/product/selectAllProduct.do',
        //fit:true,
        fitColumns: true,
        columns: [[
            {field: 'id', hidden: true},
            {field: 'productName', title: '保险产品', width: 100},
            {field: 'sn', title: '保险编号', width: 100},
            {field: 'annualFee', title: '保险年费', width: 100},
        ]],
    })
    record_dialog.dialog({
        closed:true,
        title: '查看商品信息',
        width: 700,
        height: 600,
        buttons:'#dialog_buttons'
    })
    record_datagrid.datagrid({
        url: "/temporary/queryRecord.do",
        fitColumns: true, // 自适应列
        fit: true,
        striped: true,  // 斑马线
        pagination: true,
        pageSize: 10,
        pageNumber: 1,
        singleSelect: true, // 单选
        toolbar: "#record_toolbar",
        columns: [
            [
                {field: 'id', hidden: true},
                {field: 'sn', title: '保单编号', width: 80},
                {
                    field: 'client', title: '投保人', width: 80, formatter: function (value, row) {
                        if (row.client) {
                            return row.client.name
                        }
                    }
                },
                {
                    field: 'car', title: '车牌号', width: 80, formatter: function (value, row) {
                        if (row.car) {
                            return row.car.carNum
                        }
                    }
                },
                {field: 'applyDate', title: '申请日期', width: 80},
                {field: 'auditDate', title: '核保日期', width: 80},
                {field: 'beginDate', title: '开始日期', width: 80},
                {field: 'endDate', title: '保险截止日', width: 80},
                {
                    field: 'time', title: '保险时长', width: 80
                },
                {
                    field: 'inputUser', title: '业务员', width: 80, formatter: function (value, row) {
                        if (row.inputUser) {
                            return row.inputUser.username
                        }
                    }
                },
                {
                    field: 'auditUser', title: '审核人', width: 80, formatter: function (value, row) {
                        if (row.auditUser) {
                            return row.auditUser.username
                        }
                    }
                },
                {field: 'totalAmount', title: '投保总金额', width: 80},
                {
                    field: 'state', title: '状态', width: 80, formatter: function (value) {
                        if (value) {
                            if (value == 0) {
                                return '拒保';
                            }
                            if (value == 1) {
                                return '暂存';
                            }
                            if (value == 2) {
                                return '提交未审核';
                            }
                            if (value == 3) {
                                return '退回修改';
                            }
                            if (value == 4) {
                                return '已审核未交费';
                            }
                            if (value == 5) {
                                return '已交费';
                            }
                        }
                    }
                },
                {
                    field: 'item', title: '查看明细', width: 80, formatter: function () {
                        return '<a class="showProduct">查看产品明细</a>';
                    }
                }
            ]]
    })
    $("#myDiv").on("click", "a.showProduct", function () {
        //获取当前选中的行
        var row = record_datagrid.datagrid('getSelected');
        //回显
        $.get('/car/selectByInsureId.do', {insureId: row.id}, function (data) {
            $("input[tag='carOwnName']").val(data.owner)
            $("input[tag='carNum']").val(data.carNum)
            $("input[tag='buyDate']").val(data.purchaseDate)
            $("input[tag='brand']").val(data.brand)
            $("input[tag='model']").val(data.productName)
            $("input[tag='exhaust']").val(data.displacement)
            $("input[tag='price']").val(data.evaluation)
            $("input[tag='carClass']").val(data.type)
            $("input[tag='size']").val(data.size)
        })

        //回显保险时长
        $("input[tag='burningTime']").val(row.time)
        //回显总金额
        $("#totalAmount").html(row.totalAmount)
        //回显明细表格
        $.get('/product/selectAllProduct.do', {insureId: row.id}, function (data) {
            productItem_datagrid.datagrid('loadData', data);
            $("input[tag='organization']").val(data[0].organization.name)
        })
        record_dialog.dialog('open')
    })
    var objMethod = {
        query: function () {
            //获取高级查询的参数值
            var clientName = $("#clientName").textbox('getValue')
            var sn = $("#sn").textbox('getValue')
            var inputUserName = $("#inputUserName").textbox('getValue');
            //重新加载数据表格
            record_datagrid.datagrid('load', {
                clientName: clientName,
                sn: sn,
                inputUserName: inputUserName
            });
        },
        cancel:function () {
            record_dialog.dialog('close')
        },
    }
    // 绑定事件
    $('a[data-cmd]').click(function () {
        objMethod[$(this).data('cmd')]();
    })
})